import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {
  fang_office,
  fang_office_rental,
  fang_rental,
  fang_shops_rental,
} from '@/api/office';

const officeModel = {
  namespace: 'officeModel',

  state: {
    list: JSON.parse(localStorage.getItem('list')) || [],
  },

  effects: {
    *fang_Office({ payload }, { call, put }) {
      const data = yield call(fang_office);
      // console.log(data.data.RECORDS, 'data111');
      console.log('11111');
      yield put({ type: 'fang_OfficeListInit', payload: data.data.RECORDS });
    },
    *fang_Office_rental({ payload }, { call, put }) {
      console.log('22222');
      const data = yield call(fang_office_rental);
      console.log(data.data.RECORDS, 'data111');
      yield put({
        type: 'fang_office_rentalListInit',
        payload: data.data.RECORDS,
      });
    },
    *fang_rental({ payload }, { call, put }) {
      console.log('23223');
      const data = yield call(fang_rental);
      //console.log(data.data.RECORDS, 'data111');
      yield put({ type: 'fang_rentaleListInit', payload: data.data.RECORDS });
    },
    *fang_shops_rental({ payload }, { call, put }) {
      console.log('11111');
      const data = yield call(fang_shops_rental);
      //console.log(data.data.RECORDS, 'data111');
      yield put({
        type: 'fang_shops_rentalListInit',
        payload: data.data.RECORDS,
      });
    },
  },
  reducers: {
    //
    fang_OfficeListInit(state, { payload }) {
      state.list = payload;
      state.list = [...state.list];
      // console.log(state.list, 'list ');
      localStorage.setItem('list', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    fang_office_rentalListInit(state, { payload }) {
      console.log(payload, 'sssssssssssss');
      state.list = payload;
      state.list = [...state.list];
      // console.log(state.list, 'list ');
      localStorage.setItem('list', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    fang_rentaleListInit(state, { payload }) {
      console.log(payload, 'sssssssssssss');
      state.list = payload;
      state.list = [...state.list];
      // console.log(state.list, 'list ');
      localStorage.setItem('list', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    fang_shops_rentalListInit(state, { payload }) {
      console.log(payload, 'sssssssssssss');
      state.list = payload;
      state.list = [...state.list];
      // console.log(state.list, 'list ');
      localStorage.setItem('list', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    //组件删除
    DEL_List(state, { payload }) {
      state.list.forEach((item, index) => {
        if (payload.id == item.id) {
          console.log('22222222');
          state.list.splice(index, 1);
        }
      });
      state.list = [...state.list];
      localStorage.setItem('list', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    EDIT_LIST(state, { payload }) {
      console.log(payload, 'ssssssssssssss');
      state.list.forEach((item, index) => {
        if (payload.id == item.id) {
          state.list[index] = payload;
        }
      });
      localStorage.setItem('list', JSON.stringify(state.list));
      return {
        ...state,
      };
    },
    search_list(state, { payload }) {
      console.log(state.list, 'arrr');
      state.list = state.list.filter((item) => item.title.includes(payload));
      state.list = [...state.list];
      return {
        ...state,
      };
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default officeModel;
